package cn.zane.designPattern.createPatterns.abstractFactoryPatterns.design.factory;

import cn.zane.designPattern.createPatterns.abstractFactoryPatterns.design.util.ClassLoaderUtils;
import cn.zane.designPattern.createPatterns.abstractFactoryPatterns.design.workshop.ICacheAdapter;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author: zane
 * @Date: 2024/12/29
 * @Description: 提供Redis工厂的工厂（代理抽象工厂->提供集群类型的Redis工厂【ICacheAdapter】->提供Redis服务【EGM/IIR】）
 */
public class JDKInvocationHandler implements InvocationHandler {

    private final ICacheAdapter cacheAdapter;

    public JDKInvocationHandler(ICacheAdapter cacheAdapter) {
        this.cacheAdapter = cacheAdapter;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return ICacheAdapter.class
                .getMethod(method.getName(), ClassLoaderUtils.getClazzByArgs(args))
                .invoke(cacheAdapter, args);
    }
}
